package cn.edu.jxau.core.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import cn.edu.jxau.core.exception.ServiceException;
import cn.edu.jxau.core.util.DateUtils;
import cn.edu.jxau.framework.dao.core.Session;
import cn.edu.jxau.framework.dao.core.sqlSessionFactory;

public class SpectrumTypeService extends ServiceSupport{

	private static Logger logger = LoggerFactory.getLogger(SpectrumTypeService.class);
	public SpectrumTypeService() {
		
	}
    public SpectrumTypeService(String view, Map<String, Object[]> params, Map<String, Object> data) throws ServiceException {
        super(view, params, data);
        execute(view, params, data); // 执行业务
    }
    
    /**
     *添加光谱类型
     * 
     * */
	private void addType(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的 

        String spectrumType = getParamStringValue(params, "spectrumType", 0);

       
        //数据库操作
        
        //插入数据
        param.put("spectrumType", spectrumType);
        try {
            Session session = sqlSessionFactory.getInstance().getSession();
            session.execute("addType", param, datas);
            if(null == spectrumType){
            	data.put("result",FAILED);
            }
            else{
            	 data.put("result",SUCCESS);
            }
            //释放会话
            session.releaseConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * 查询所有的光谱类型
	 * */
	private void listAllType (Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的 
        
        try {
            Session session = sqlSessionFactory.getInstance().getSession();
            session.execute("listAllType", param, datas);

            
            if(null == datas || datas.isEmpty()){
            	data.put("result", FAILED);
            }
            else{
            	//保存session的数据
            	data.put("data", datas);
                data.put("result", SUCCESS);
        		logger.debug(JSON.toJSONString(datas));
            }
            //释放会话
            session.releaseConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * 修改光谱类型
	 * 
	 * */
	@SuppressWarnings("unused")
	private void updateType(Map<String, Object[]> params, Map<String, Object> data){
        Map<String, Object> param = new HashMap<String, Object>(); //用来封装传递给数据库的参数
        List<HashMap<String, Object>> datas = new ArrayList();  // 用来接收数据库返回回来的 
        
        String Id = getParamStringValue(params, "Id", 0);
        String spectrumType = getParamStringValue(params, "spectrumType", 0);
        
        //修改操作
        param.put("spectrumType", spectrumType);
        param.put("Id", Id);
        try {
            Session session = sqlSessionFactory.getInstance().getSession();
            session.execute("updateType", param, datas);
            if(null == spectrumType){
            	data.put("result",FAILED);
            }
            else{
            	 data.put("result",SUCCESS);
            }
            data.put("result", SUCCESS);
            //释放会话
            session.releaseConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}
 